package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.vo.MdmDeviceSumVO;
import com.kaili.basic.pojo.vo.MdmDeviceVO;
import com.kaili.basic.pojo.vo.MdmDeviceSafetyVo;
import com.kaili.basic.service.IDeviceService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.aop.OperateLogAspect;
import com.kaili.common.aop.log.enums.FunctionName;
import com.kaili.common.aop.log.enums.OperateType;
import com.kaili.common.response.RestStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.exception.RestException;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;


/**
*  控制器
* @author:zhouyijian
* @Time: 2019-07-24
* @Copyright: ©  杭州凯立通信有限公司 版权所有
* @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
*/
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "设备登记", description = "设备登记相关API")
public class MdmDeviceController {
    @Autowired
	IDeviceService deviceService;

	@GetMapping(value = "/device/{id}")
	@ApiOperation("根据ID查询设备登记详情")
	public MdmDeviceVO ById(@ApiParam("ID") @PathVariable("id") Integer id) {
		return deviceService.getDeviceById(id);
	}

	@GetMapping(value = "/device")
	@ApiOperation("设备登记分页查询")
	public Page<MdmDeviceVO> queryPageList(@ModelAttribute MdmDeviceQueryDto dto) {
		Page<MdmDeviceVO> result =deviceService.queryPageList(dto);
		return result;
	}

	@GetMapping(value = "/device/modelList/{typeId}")
	@ApiOperation("设备型号查询")
	public List<MdmDeviceVO> queryDeviceModelList(@ApiParam("typeId") @PathVariable("typeId")Integer typeId) {
		List<MdmDeviceVO> result =deviceService.queryModelByTypeId(typeId);
		return result;
	}

    @PostMapping(value = "/device/add")
	@ApiOperation("设备登记/注册")
	@OperateLogAspect(functionName = FunctionName.deviceRegister,operateType = OperateType.ADD, key = "imei")
	public RestStatus addDevice(@RequestBody  @Validated MdmDeviceAddDto dto) {
		return deviceService.doCreateDevice(dto);
	}

	@PostMapping(value = "/device/pdaAdd")
	@ApiOperation("PDA设备登记/注册")
	@OperateLogAspect(functionName = FunctionName.deviceRegister,operateType = OperateType.ADD, key = "imei")
	public RestStatus pdaAddDevice(@RequestBody  @Validated MdmDeviceAddDto dto) {
		return deviceService.doCreateDevice(dto);
	}


    @PutMapping(value = "/device/edit")
    @ApiOperation("设备编辑")
	@OperateLogAspect(functionName = FunctionName.deviceRegister,operateType = OperateType.UPDATE, key = "imei")
	public RestStatus update(@RequestBody @Validated MdmDeviceAddDto dto) {
		return deviceService.updateDeviceByImei(dto);

    }

	@PutMapping(value = "/device/switchDeviceStatus/{imei}")
	@ApiOperation("设备启用/停用")
	@OperateLogAspect(functionName = FunctionName.deviceRegister,operateType = OperateType.UPDATE, key = "imei")
	public RestStatus switchFacilityStatus(@ApiParam("imei") @PathVariable("imei") String imei) {
		return deviceService.switchDeviceStatus(imei);

	}



    @DeleteMapping(value = "/device/{id}")
    @ApiOperation("删除")
	@OperateLogAspect(functionName = FunctionName.deviceRegister,operateType = OperateType.DELETE)
	public RestStatus del(@ApiParam("id") @PathVariable("id") Integer id) {
		return deviceService.deleteByImei(id);
    }

	@PostMapping(value = "/device/import")
	@ApiOperation("设备登记信息导入")
	public void importExcel( @Validated @RequestParam ("file") MultipartFile file) throws Exception {
		List<MdmDeviceImportDTO> list = ExcelUtil.importExcel(file, 1, 1, MdmDeviceImportDTO.class);

		for(MdmDeviceImportDTO dto : list){
			 deviceService.doCreateDeviceImport(dto);
		}

	}

	@GetMapping(value = "/device/export")
	@ApiOperation("设备登记信息导出")
	public void export(HttpServletResponse response, @ModelAttribute MdmDeviceQueryDto dto) throws RestException {
		List<MdmDeviceVO> list = deviceService.queryListByCondition(dto);
		ExcelUtil.exportExcel(list,"设备登记信息","", MdmDeviceVO.class, UUID.randomUUID().toString()+".xls",response);
	}

	@GetMapping(value = "/device/index")
	@ApiOperation("首页")
	public MdmDeviceSumVO queryDeviceSumList(MdmDeviceSumDTO dto) {
		MdmDeviceSumVO result =deviceService.queryDeviceSumList(dto);
		return result;
	}

	/**
	 * @Desc:  批量查询设备信息
	 * @author: shenliqiang
	 * @Time: 2019/7/25 13:40
	 * @param mdmDeviceSafetyDto
	 * @return com.baomidou.mybatisplus.plugins.Page<com.kaili.basic.pojo.vo.MdmDeviceOnlineVo>
	 */

	@GetMapping(value = "/device/queryDevicesInfo")
	@ApiOperation(value = "条件查询在线设备信息--web",notes = "条件查询在线设备信息，列表界面")
	public Page<MdmDeviceSafetyVo> queryonlineDeviceInfo(@ModelAttribute @Validated MdmDeviceSafetyDto mdmDeviceSafetyDto) {
		return  deviceService.queryMdmDeviceSafetyInfo(new Page<>(mdmDeviceSafetyDto.getPage(), mdmDeviceSafetyDto.getPageSize()), mdmDeviceSafetyDto);
	}
}
